unit uMiscelania;

interface

uses
    SysUtils, db,
    Controls, ComCtrls, Forms,
    cxGridDBTableView, Windows,
    Generics.Collections, cxGrid,
    cxButtons, dxBar;

type
  TMiscelania = class

    {$REGION '<< Atributes >>'}

      strict private class var _Miscelania: TMiscelania;

    {$ENDREGION}

    {$REGION '<< Methods >>'}

      public class function GetInstance(): TMiscelania;
      public class procedure FreeInstance();

      public function IsNull(const a_String: String): Boolean;
      public function KeyValidateCode(const a_Char: Char): Char;
      public function InEditing(Controle: TcxGridDbTableView): Boolean;

      public procedure ConfigureStateChange(Controle: TcxGridDbTableView; btCriar, btAlterar, btExcluir, btSair: TdxBarLargeButton);

    {$ENDREGION}

  end;

implementation

{$REGION '<< TMiscelania >>'}

procedure TMiscelania.ConfigureStateChange(Controle: TcxGridDbTableView; btCriar, btAlterar, btExcluir, btSair: TdxBarLargeButton);
begin
  if Assigned(btCriar)
    then btCriar.Enabled := (not (Controle.DataController.Filter.IsFiltering)) and (Controle.DataController.DataSource.State in [dsBrowse]) and (Controle.OptionsData.Editing);

  if Assigned(btAlterar)
    then btAlterar.Enabled := (Controle.DataController.DataSource.State in [dsBrowse]) and (Controle.DataController.RowCount > 0) and (Controle.DataController.DataSource.DataSet.RecordCount > 0) and (Controle.OptionsData.Editing);

  if Assigned(btExcluir)
    then btExcluir.Enabled := (Controle.DataController.DataSource.State in [dsBrowse]) and (Controle.DataController.RowCount > 0) and (Controle.DataController.DataSource.DataSet.RecordCount > 0) and (Controle.OptionsData.Editing);

  if Assigned(btSair)
    then btSair.Enabled := (Controle.DataController.DataSource.State in [dsBrowse]);
end;

class procedure TMiscelania.FreeInstance;
begin
  if Assigned(_Miscelania)
    then FreeAndNil(_Miscelania);
end;

class function TMiscelania.GetInstance: TMiscelania;
begin
  if not Assigned(_Miscelania)
    then _Miscelania := inherited Create as Self;

  Result := _Miscelania;
end;

function TMiscelania.InEditing(Controle: TcxGridDbTableView): Boolean;
begin
  Result := Controle.DataController.DataSource.State in [dsEdit, dsInsert];
end;

function TMiscelania.IsNull(const a_String: String): Boolean;
begin
  Result := Trim(a_String) = '';
end;

function TMiscelania.KeyValidateCode(const a_Char: Char): Char;
begin
  Result := a_Char;
  if not (a_Char in ['A'..'Z', 'a'..'z', '0'..'9', #8])
    then Result := #0;
end;

{$ENDREGION}

end.
